import store from '@/store';
import Vue from 'vue';
import VueRouter from 'vue-router';
Vue.use(VueRouter);
// 引入view
// 登录
const myLogin = () => import('@/views/myLogin.vue');
// 注册
const myRegister = () => import('@/views/myRegister.vue');
// 主页
const myIndex = () => import('@/views/myIndex.vue');
// welcome
const myWelcome = () => import('@/views/myWelcome.vue');
// 首页
const myHome = () => import('@/views/home/myHome.vue');
// 项目
const myProject = () => import('@/views/project/myProject.vue');
// 论文
const myEssay = () => import('@/views/essay/myEssay.vue');
// 竞赛
// const myContest = () => import('@/views/contest/myContest.vue');
// 获奖
const myAward = () => import('@/views/award/myAward.vue');
// 教材与著作
const myWork = () => import('@/views/work/myWork.vue');
// 专利
const myPatent = () => import('@/views/patent/myPatent.vue');
// 管理者 ******************************************************************************
const myAdmin = () => import('@/views/myAdmin.vue');
// 用户列表
const myUserList = () => import('@/views/admin/myUserList');
// 注册管理
const myCrewManage = () => import('@/views/admin/myCrewManage');
// 业绩列表
const myAchievementList = () => import('@/views/admin/myAchievementList');
// 业绩办理
const myAchievementTransaction = () =>
	import('@/views/admin/myAchievementTransaction');
const routes = [
	{
		path: '/',
		redirect: '/login',
	},
	{
		name: 'login',
		path: '/login',
		component: myLogin,
	},
	{
		name: 'register',
		path: '/register',
		component: myRegister,
	},
	// 普通用户
	{
		name: 'index',
		path: '/index',
		meta: { requireAuth: true },
		component: myIndex,
		redirect: '/index/welcome',
		children: [
			{
				name: 'welcome',
				path: 'welcome',
				component: myWelcome,
				meta: { requireAuth: true },
			},
			{
				name: 'home',
				path: 'home',
				component: myHome,
				meta: { requireAuth: true },
			},
			{
				name: 'project',
				path: 'project',
				component: myProject,
				meta: { requireAuth: true },
			},
			{
				name: 'essay',
				path: 'essay',
				component: myEssay,
				meta: { requireAuth: true },
			},
			{
				name: 'award',
				path: 'award',
				component: myAward,
				meta: { requireAuth: true },
			},
			{
				name: 'work',
				path: 'work',
				component: myWork,
				meta: { requireAuth: true },
			},
			{
				name: 'patent',
				path: 'patent',
				component: myPatent,
				meta: { requireAuth: true },
			},
		],
	},
	// 管理者
	{
		name: 'admin',
		path: '/admin',
		component: myAdmin,
		redirect: '/admin/welcome',
		children: [
			{
				name: 'welcome',
				path: 'welcome',
				component: myWelcome,
				meta: { requireAuth: true },
			},
			{
				name: 'userList',
				component: myUserList,
				path: 'userList',
				meta: { requireAuth: true },
			},
			{
				name: 'crewManage',
				component: myCrewManage,
				path: 'crewManage',
				meta: { requireAuth: true },
			},
			{
				name: 'achievementList',
				component: myAchievementList,
				path: 'achievementList',
				meta: { requireAuth: true },
			},
			{
				name: 'achievementTransaction',
				component: myAchievementTransaction,
				path: 'achievementTransaction',
				meta: { requireAuth: true },
			},
		],
	},
];

const router = new VueRouter({
	mode: 'history',
	base: process.env.BASE_URL,
	routes,
});
let vm = new Vue();
router.beforeEach((to, from, next) => {
	if (to.meta.requireAuth) {
		if (store.state.token) {
			// console.log('test');
			next();
		} else {
			next({
				name: 'login',
				params: {
					redirect: to.fullPath,
				},
			});
			vm.$message.warning('你还未登录');
		}
	} else {
		next();
	}
});
export default router;
